package cn.itcast.p2p20.fragments;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.otto.Subscribe;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.itcast.p2p20.R;
import cn.itcast.p2p20.act.AssetsActivity;
import cn.itcast.p2p20.act.LoginActivity;
import cn.itcast.p2p20.act.MyApplication;
import cn.itcast.p2p20.act.PayActivity;
import cn.itcast.p2p20.act.ProfitActivity;
import cn.itcast.p2p20.act.RateActivity;
import cn.itcast.p2p20.bean.UserBean;
import cn.itcast.p2p20.utils.Constant;
import cn.itcast.p2p20.utils.Util;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment {


    @InjectView(R.id.me_header)
    ImageView meHeader;
    @InjectView(R.id.me_name)
    TextView meName;
    private UserBean userInfo;

    @Override
    protected void initData() {

        showSuccessStateLayout();

        //视图从缓存获取用户数据信息
        userInfo = Util.getUserInfo(mContext);
        setUserInfo(userInfo);

    }

    private void setUserInfo(UserBean userInfo) {
        if (userInfo != null) {

            meName.setText(userInfo.getUsername());

            String headUrl = userInfo.getHeadUrl();
            Picasso.with(mContext).load(Constant.format(headUrl)).transform(new Transformation() {

                //就是加载图片之后生成的bitmap，
                //方法返回的是什么bitmap图片那么就显示什么bitmap图片

                // java.lang.IllegalStateException: Transformation key mutated input Bitmap but failed to recycle the original.
                //返回新的Bitmap 需要将原来的bitmap 回收
                @Override
                public Bitmap transform(Bitmap source) {

                    int width = source.getWidth();
                    int height = source.getHeight();

                    //Bitmap createBitmap(int width, int height, Config config)
                    Bitmap bitmap = Bitmap.createBitmap(width, height, source.getConfig());
                    Canvas canvas = new Canvas(bitmap);
                    //drawCircle(float cx, float cy, float radius, @NonNull Paint paint)
                    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                    //paint.setColor(Color.RED);
                    //通过遮罩模式把Bitmap source绘制到新的Bitmap
                    //BitmapShader(@NonNull Bitmap bitmap, TileMode tileX, TileMode tileY)
                    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                    canvas.drawCircle(width * 0.5f, height * 0.5f, Math.min(width, height) * 0.5f, paint);


                    source.recycle();
                    return bitmap;
                }

                //Transformation key must not be null
                @Override
                public String key() {
                    return "key";
                }

            }).into(meHeader);
        }
    }

    @Override
    protected void initView(View childFragmentView) {

        ButterKnife.inject(this, childFragmentView);
        //订阅事件
        ((MyApplication) getActivity().getApplication()).getBus().register(this);
    }

    @Override
    protected String getTitle() {
        return null;
    }

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_me;
    }

    @OnClick({R.id.me_name, R.id.me_deposit, R.id.me_withdraw_deposit, R.id.me_my_assets, R.id.me_my_profit, R.id.me_my_yield})
    public void onClick(View view) {
        switch (view.getId()) {

            ////点击如果没有登录就跳转到登录界面去登录
            case R.id.me_name:

                if (userInfo == null) {
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    mContext.startActivity(intent);
                }

                break;
            //充值
            case R.id.me_deposit:

                Intent intent3 = new Intent(mContext, PayActivity.class);
                mContext.startActivity(intent3);

                break;
            case R.id.me_withdraw_deposit:


                break;
            //资产分布
            case R.id.me_my_assets:

                Intent intent = new Intent(mContext, AssetsActivity.class);
                //mContext.startActivity(intent);
                startActivityForResult(intent, Constant.REQUEST_FOR_ASSETS);

                break;
            //每日收益
            case R.id.me_my_profit:

                Intent intent1 = new Intent(mContext, ProfitActivity.class);
                //mContext.startActivity(intent1);
                startActivityForResult(intent1, Constant.REQUEST_FOR_PROFIT);

                break;
            //年化收益率变化
            case R.id.me_my_yield:

                Intent intent2 = new Intent(mContext, RateActivity.class);
                //mContext.startActivity(intent2);
                startActivityForResult(intent2, Constant.REQUEST_FOR_RATE);

                break;
        }
    }


    //得到从资产分布界面，每日收益界面，年化收益界面跳转到的登录界面成功之后得到的结果
    //登录成功之后在这里重新开启资产界面或者每日收益界面或者年化收益界面
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == Constant.RESULT_LOGIN_SUCCESS && requestCode == Constant.REQUEST_FOR_ASSETS) {

            Intent intent = new Intent(mContext, AssetsActivity.class);
            startActivity(intent);

        } else if (resultCode == Constant.RESULT_LOGIN_SUCCESS && requestCode == Constant.REQUEST_FOR_PROFIT) {

            Intent intent1 = new Intent(mContext, ProfitActivity.class);
            startActivity(intent1);

        } else if(resultCode == Constant.RESULT_LOGIN_SUCCESS && requestCode == Constant.REQUEST_FOR_RATE) {

            Intent intent2 = new Intent(mContext, RateActivity.class);
            startActivity(intent2);
        }
    }

    //得到登录成功的通知
    @Subscribe
    public void onLogin(UserBean user) {

        meName.setText(user.getUsername());

        userInfo = user;
        //登录成功保存用户名和头像地址
        Util.saveUserInfo(mContext, user);

        setUserInfo(userInfo);

    }


}
